<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>区域设置</title>
		<!-- 导入jquery核心类库 -->
		<script type="text/javascript" src="../../js/jquery-1.8.3.js"></script>
		<!--导入文件上传需要的js-->
		<script type="text/javascript" src="../../js/ocupload/jquery.ocupload-1.1.2.js" ></script>
		<!-- 导入easyui类库 -->
		<link rel="stylesheet" type="text/css" href="../../js/easyui/themes/default/easyui.css">
		<link rel="stylesheet" type="text/css" href="../../js/easyui/themes/icon.css">
		<link rel="stylesheet" type="text/css" href="../../js/easyui/ext/portal.css">
		<link rel="stylesheet" type="text/css" href="../../css/default.css">
		<script type="text/javascript" src="../../js/easyui/jquery.easyui.min.js"></script>
		<script type="text/javascript" src="../../js/easyui/ext/jquery.portal.js"></script>
		<script type="text/javascript" src="../../js/easyui/ext/jquery.cookie.js"></script>
		<script src="../../js/easyui/locale/easyui-lang-zh_CN.js" type="text/javascript"></script>
		
		<script type="text/javascript">
		   $.fn.serializeJson=function(){  
		            var serializeObj={};  
		            var array=this.serializeArray();  
		            var str=this.serialize();  
		            $(array).each(function(){  
		                if(serializeObj[this.name]){  
		                    if($.isArray(serializeObj[this.name])){  
		                        serializeObj[this.name].push(this.value);  
		                    }else{  
		                        serializeObj[this.name]=[serializeObj[this.name],this.value];  
		                    }  
		                }else{  
		                    serializeObj[this.name]=this.value;   
		                }  
		            });  
		            return serializeObj;  
		        }; 
			function doAdd(){
				
				$('#addWindow').window("open");
			}
			
			function doView(){
				//数据页面就会显
				var rows=$("#grid").datagrid('getSelections');
				if(rows.length!=1){
					$.messager.alert("提示","只能选择一行数据","warning");
				}else{
					var row=rows[0];
					
					$("#areaForm").form('load',row);
					//打开窗口
					$("#addWindow").window('open');
				}
				
			}
			
			function doDelete(){//tag
				//就是批量作废
				var rows=$("#grid").datagrid('getSelections');
				if(rows.length<1){
					$.messager.alert("提示","至少选择一行数据","warning");
				}else{
					var array=new Array();
					for (var i=0;i<rows.length;i++) {
						array.push(rows[i].id);
					}
					var ids=array.join(",");
					//if(tag==1){
					   window.location.href="../../area_delflag.action?tag=1&ids="+ids;
					//}
				}
			}
			function doBack(){
				var rows=$("#grid").datagrid('getSelections');
				if(rows.length<1){
					$.messager.alert("提示","至少选择一行数据","warning");
				}else{
					var array=new Array();
					for (var i=0;i<rows.length;i++) {
						array.push(rows[i].id);
					}
					var ids=array.join(",");
					//if(tag==0){
					   window.location.href="../../area_delflag.action?tag=0&ids="+ids;
					//}
				}
			}
			function doSearch(){
				$("#areaForm").form('clear');
				$("#searchWindow").window('open');
			}
			
			//工具栏
			var toolbar = [ {
				id : 'button-edit',	
				text : '修改',
				iconCls : 'icon-edit',
				handler : doView
			}, {
				id : 'button-add',
				text : '增加',
				iconCls : 'icon-add',
				handler : doAdd
			}, {
				id : 'button-delete',
				text : '删除',
				iconCls : 'icon-cancel',
				handler : doDelete
			},{
				id : 'button-search',
				text : '查询',
				iconCls : 'icon-search',
				handler : doSearch
			}, {
				id : 'button-back',
				text : '还原',
				iconCls : 'icon-back',
				handler : doBack
			}, {
				id : 'button-import',
				text : '导入',
				iconCls : 'icon-redo',
			}];
			// 定义列
			var columns = [ [ {
				field : 'id',
				checkbox : true,
			},{
				field : 'province',
				title : '省',
				width : 120,
				align : 'center'
			}, {
				field : 'city',
				title : '市',
				width : 120,
				align : 'center'
			}, {
				field : 'district',
				title : '区',
				width : 120,
				align : 'center'
			}, {
				field : 'postcode',
				title : '邮编',
				width : 120,
				align : 'center'
			}, {
				field : 'shortcode',
				title : '简码',
				width : 120,
				align : 'center'
			}, {
				field : 'citycode',
				title : '城市编码',
				width : 200,
				align : 'center'
			} , {
				field : 'deltag',
				title : '是否作废',
				width : 80,
				align : 'center',
				formatter : function(data,row, index){
					if(data==null){
						return "正常使用"
					}else if(data==1){
						return "已作废";
					}
				}
			}] ];
			
			$(function(){
				// 先将body隐藏，再显示，不会出现页面刷新效果
				$("body").css({visibility:"visible"});
				
				// 区域管理数据表格
				$('#grid').datagrid( {
					iconCls : 'icon-forward',
					fit : true,
					border : false,
					rownumbers : true,
					striped : true,
					pageList: [30,50,100],
					pagination : true,
					toolbar : toolbar,
					url : "../../area_search.action",
					idField : 'id',
					columns : columns,
					onDblClickRow : doDblClickRow
				});
				
				// 添加、修改区域窗口
				$('#addWindow').window({
			        title: '添加修改区域',
			        width: 400,
			        modal: true,
			        shadow: true,
			        closed: true,
			        height: 400,
			        resizable:false
			    });
			    
			    //查询窗口
			    $('#searchWindow').window({
			        title: '查询区域',
			        width: 400,
			        modal: true,
			        shadow: true,
			        closed: true,
			        height: 400,
			        resizable:false
			    });
			    
			    $("#save").click(function(){
			    	//判断是否有出错的
			    	if($("#areaForm").form('validate')){
			    		//表单提交
			    		$("#areaForm").submit();
			    	}else{
			    		$.messager.alert("警告","有违反规则的信息","warning");
			    	}
			    	
			    });
			    $("#searchBtn").click(function(){
			    	
			    	//把参数转换为json
		    		var params= $("#searchForm").serializeJson();
		    		//绑定datagrid
		    		$("#grid").datagrid('load',params);
		    		//关闭窗口
		    		$("#searchWindow").window('close');
			    });
			    
			    $("#button-import").upload({
			    	//upload的；里有一些属性
			    	//name 文件名字，enctype="multipart/form-data",autoSubmit是否自动提交，params：携带的其他的参数 ，onSubmit提交已发时间，onSelect文件选择后还未提交时
			    	//onComplete:响应回来的数据，action路径
			    	action :"../../area_batchImport.action",
			    	//但是上传文件，要规定一定的规则，只要什么后缀格式的
			    	onSelect:function(){
			    		//选中文件后关闭自动提交
			    		this.autoSubmit=false;
			    		//判定文件格式，以.xls以及.xlsx结尾的
			    		//获得文件的名字，
			    		var filename=this.filename();
			    		//编写正则
			    		var regex=/^.*\.(xls|xlsx)$/;
			    		//判断是否满足调教
			    		if(regex.test(filename)){
			    			//满足就提交
			    			this.submit();
			    		}else{
			    			$.messager.alert("上传提示","你选择的文件不符合标准","warning");
			    		}
			    	},
			    	onComplete:function(response){
			    		//相应回数据，刷新（重新加载）
			    		$("#grid").datagrid('reload');
			    	}
				});
				
			});
		
			function doDblClickRow(){
				alert("双击表格数据...");
			}
		</script>
	</head>

	<body class="easyui-layout" style="visibility:hidden;">
		<div region="center" border="false">
			<table id="grid"></table>
		</div>
		<div class="easyui-window" title="区域添加修改" id="addWindow" collapsible="false" minimizable="false" maximizable="false" style="top:20px;left:200px">
			<div region="north" style="height:31px;overflow:hidden;" split="false" border="false">
				<div class="datagrid-toolbar">
					<a id="save" icon="icon-save" href="#" class="easyui-linkbutton" plain="true">保存</a>
				</div>
			</div>

			<div region="center" style="overflow:auto;padding:5px;" border="false">
				<form id="areaForm" action="../../area_save.action" method="post">
					<table class="table-edit" width="80%" align="center">
						<tr class="title">
							<td colspan="2">区域信息</td>
						</tr>
						<tr>
							<td>编码</td>
							<td>
								<input type="text" name="id" class="easyui-validatebox" required="true" />
							</td>
						</tr>
						<tr>
							<td>省</td>
							<td>
								<input type="text" name="province" class="easyui-validatebox" required="true" />
							</td>
						</tr>
						<tr>
							<td>市</td>
							<td>
								<input type="text" name="city" class="easyui-validatebox" required="true" />
							</td>
						</tr>
						<tr>
							<td>区</td>
							<td>
								<input type="text" name="district" class="easyui-validatebox" required="true" />
							</td>
						</tr>
						<tr>
							<td>邮编</td>
							<td>
								<input type="text" name="postcode" class="easyui-validatebox" required="true" />
							</td>
						</tr>
						<tr>
							<td>简码</td>
							<td>
								<input type="text" name="shortcode" class="easyui-validatebox" required="true" />
							</td>
						</tr>
						<tr>
							<td>城市编码</td>
							<td>
								<input type="text" name="citycode" class="easyui-validatebox" required="true" />
							</td>
						</tr>
					</table>
				</form>
			</div>
		</div>
		
		<!-- 查询区域-->
		<div class="easyui-window" title="查询区域窗口" closed="true" id="searchWindow" collapsible="false" minimizable="false" maximizable="false" style="width: 400px; top:40px;left:200px">
			<div style="overflow:auto;padding:5px;" border="false">
				<form id="searchForm">
					<table class="table-edit" width="80%" align="center">
						<tr class="title">
							<td colspan="2">查询条件</td>
						</tr>
						<tr>
							<td>省份</td>
							<td>
								<input type="text" name="province" />
							</td>
						</tr>
						<tr>
							<td>城市</td>
							<td>
								<input type="text" name="city" />
							</td>
						</tr>
						<tr>
							<td>区（县）</td>
							<td>
								<input type="text" name="district" />
							</td>
						</tr>
						<tr>
							<td colspan="2"><a id="searchBtn" href="#" class="easyui-linkbutton" data-options="iconCls:'icon-search'">查询</a> </td>
						</tr>
					</table>
				</form>
			</div>
		</div>
	</body>

</html>